﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;

namespace Seeker.AdminUI
{
    /// <summary>
    /// WebService地图
    /// </summary>
    public class ServiceMaps
    {
        public Dictionary<string, string> servicesList = new Dictionary<string, string>();

        public ServiceMaps()
        {
            servicesList.Add("/UserService", "/Services/AUserService.asmx");
        }

        /// <summary>
        /// 重写URL
        /// </summary>
        /// <param name="context">请求对象 HttpContext</param>
        /// <param name="originUrl">url地址</param>
        /// <param name="serviceAbsolutePath">物理真实地址</param>
        /// <param name="target">目标</param>
        public void RewriteUrl(HttpContext context, string originUrl, string serviceAbsolutePath, string target)
        {
            string contextRequest = HttpContext.Current.Request.Url.AbsolutePath;

            if (contextRequest.Contains(originUrl))
            {
                serviceAbsolutePath = serviceAbsolutePath + "/" + target;
                int dotasmx = serviceAbsolutePath.IndexOf(".asmx");
                string path = serviceAbsolutePath.Substring(0, dotasmx + 5);
                string pathInfo = serviceAbsolutePath.Substring(dotasmx + 5);

                if (context.Request.HttpMethod == "GET")
                {
                    int len = context.Request.Url.Query.Length;
                    string queryString = len > 1 ? context.Request.Url.Query.Substring(1, len - 1) : "";
                    context.RewritePath(path, pathInfo, queryString);
                }
                else if (context.Request.HttpMethod == "POST")
                {
                    context.RewritePath(path, pathInfo, context.Request.Url.Query);
                }
            }
        }

        /// <summary>
        /// 重写URL
        /// </summary>
        /// <param name="context">请求对象 HttpContext</param>
        /// <param name="originUrl">url地址</param>
        /// <param name="serviceAbsolutePath">物理真实地址</param>
        public void RewriteUrl(HttpContext context, string originUrl, string serviceAbsolutePath)
        {
            RewriteUrl(context, originUrl, serviceAbsolutePath, "");
        }

        /// <summary>
        /// 重写URL
        /// </summary>
        /// <param name="context">请求对象 HttpContext</param>
        public void RewriteUrl(HttpContext context)
        {
            Regex getTarget = new Regex(@"(/$)|(^[^^]*/)");
            string orginUrl = context.Request.Path;
            string absolutePath;
            bool hasValue = servicesList.TryGetValue(orginUrl, out absolutePath);

            if (hasValue)
            {
                RewriteUrl(context, orginUrl, absolutePath);
            }
            else
            {
                string target = getTarget.Replace(orginUrl, "");
                orginUrl = orginUrl.Replace("/" + target, "");
                hasValue = servicesList.TryGetValue(orginUrl, out absolutePath);
                
                if (hasValue)
                {
                    RewriteUrl(context, orginUrl, absolutePath, target);
                }
            }
        }
    }
}